package com.example.hdz.floatwindowdemo;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class CircleLayout extends ViewGroup {

    private float mAngleOffset;
    private float mAngleRange;
    private int mInnerRadius;

    private Bitmap mDst;
    private Bitmap mSrc;
    private Bitmap mDrawingCache;
    private int mMaxChildHeight = 0;

    public CircleLayout(Context context) {
        this(context, null);
    }

    public CircleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mAngleOffset = 90f;
        mAngleRange = 360f;
        mInnerRadius = 180;

        setPadding(0,0,0,0);
    }

    public CircleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CircleLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 获取子控件个数
        final int count = getChildCount();

        int maxHeight = 0;
        int maxWidth = 0;

        // Find rightmost and bottommost child
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                //某一个子view，多宽，多高, 内部加上了viewGroup的padding值
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
                maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
                maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
                mMaxChildHeight = maxHeight;
            }
        }

        // Check against our minimum height and width
        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

        int width = resolveSize(maxWidth, widthMeasureSpec);
        int height = resolveSize(maxHeight, heightMeasureSpec);

        setMeasuredDimension(width, height);

        if(mSrc != null && (mSrc.getWidth() != width || mSrc.getHeight() != height)) {
            mDst.recycle();
            mSrc.recycle();
            mDrawingCache.recycle();

            mDst = null;
            mSrc = null;
            mDrawingCache = null;
        }

        if(mSrc == null) {
            mSrc = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mDst = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mDrawingCache = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        }
    }

    private LayoutParams layoutParams(View child) {
        return (LayoutParams) child.getLayoutParams();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int childs = getChildCount();

        float totalWeight = 0f;

        for(int i=0; i<childs; i++) {
            final View child = getChildAt(i);
            LayoutParams lp = layoutParams(child);
            totalWeight += lp.weight;
        }

        final int width = getWidth();
        final int height = getHeight() + mMaxChildHeight /4;

        final float minDimen = width > height ? height : width;
        final float radius = (minDimen - mInnerRadius)/2f;


        float startAngle = mAngleOffset;

        for(int i=0; i<childs; i++) {
            final View child = getChildAt(i);

            final LayoutParams lp = layoutParams(child);

            final float angle = mAngleRange/totalWeight * lp.weight;

            final float centerAngle = startAngle + angle/2f;
            final int x;
            final int y;

            if(childs > 1) {
                x = (int) (radius * Math.cos(Math.toRadians(centerAngle))) + width/2;
                y = (int) (radius * Math.sin(Math.toRadians(centerAngle))) + height/2;
            } else {
                x = width/2;
                y = height/2;
            }

            final int halfChildWidth = child.getMeasuredWidth()/2;
            final int halfChildHeight = child.getMeasuredHeight()/2;

            final int left = lp.width != LayoutParams.MATCH_PARENT ? x - halfChildWidth : 0;
            final int top = lp.height != LayoutParams.MATCH_PARENT ? y - halfChildHeight : 0;
            final int right = lp.width != LayoutParams.MATCH_PARENT ? x + halfChildWidth : width;
            final int bottom = lp.height != LayoutParams.MATCH_PARENT ? y + halfChildHeight : height;

            child.layout(left, top, right, bottom);

            startAngle += angle;
        }
        invalidate();
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        LayoutParams lp = new LayoutParams(p.width, p.height);

        if(p instanceof LinearLayout.LayoutParams) {
            lp.weight = ((LinearLayout.LayoutParams) p).weight;
        }

        return lp;
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof LayoutParams;
    }



    public static class LayoutParams extends ViewGroup.LayoutParams {


        public float weight = 1f;

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    }
}
